﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using CCWin.SkinControl;
using System.Configuration;
using WFNetLib;
namespace 生命体征测试仪
{
    public partial class formDeviceSet : formBase
    {
        public formDeviceSet()
        {
            InitializeComponent();
        }
        private void skinButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        string[] ports;
        void SetCOMSelect(SkinComboBox cb,string com)
        {
            for (int i = 0; i < ports.Length; i++)
            {
                if (ports[i] == com)
                {
                    cb.SelectedIndex = i;
                    break;
                }
            }
        }
        private void formDeviceSet_Load(object sender, EventArgs e)
        {
            SysConfig.CloseCOM();
            DeviceTab.SelectTab(0);
            ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbShenGaoCOM.Items.Clear();
            cbShenGaoCOM.Items.AddRange(ports);
            cbTiWenCOM.Items.Clear();
            cbTiWenCOM.Items.AddRange(ports);
            cbXueYaCOM.Items.Clear();
            cbXueYaCOM.Items.AddRange(ports);
            cbXinDianCOM.Items.Clear();
            cbXinDianCOM.Items.AddRange(ports);
            cbXueYangCOM.Items.Clear();
            cbXueYangCOM.Items.AddRange(ports);
            cbXueTangCOM.Items.Clear();
            cbXueTangCOM.Items.AddRange(ports);
            cbXueZhiCOM.Items.Clear();
            cbXueZhiCOM.Items.AddRange(ports);
            cbTimerCOM.Items.Clear();
            cbTimerCOM.Items.AddRange(ports);

            SetCOMSelect(cbShenGaoCOM, SysConfig.XPComNum);
            SetCOMSelect(cbTiWenCOM, SysConfig.TiWenComNum);
            SetCOMSelect(cbXueYaCOM, SysConfig.XueYaComNum);
            SetCOMSelect(cbXinDianCOM, SysConfig.XinDianComNum);
            SetCOMSelect(cbXueYangCOM, SysConfig.XueYangComNum);
            SetCOMSelect(cbXueZhiCOM, SysConfig.XueZhiComNum);
            SetCOMSelect(cbTimerCOM, SysConfig.TimerComNum);
            SetCOMSelect(cbXueTangCOM, SysConfig.XueTangComNum);

            if (SysConfig.bHaveShenGao)
                bHaveShenGao.Checked = true;
            else
                bHaveShenGao.Checked = false;
            if (SysConfig.bHaveTiWen)
                bHaveTiWen.Checked = true;
            else
                bHaveTiWen.Checked = false;
            if (SysConfig.bHaveXueYa)
                bHaveXueYa.Checked = true;
            else
                bHaveXueYa.Checked = false;
            if (SysConfig.bHaveXinDian)
                bHaveXinDian.Checked = true;
            else
                bHaveXinDian.Checked = false;
            if (SysConfig.bHaveXueYang)
                bHaveXueYang.Checked = true;
            else
                bHaveXueYang.Checked = false;
            if (SysConfig.bHaveXueTang)
                bHaveXueTang.Checked = true;
            else
                bHaveXueTang.Checked = false;
            if (SysConfig.bHaveXueZhi)
                bHaveXueZhi.Checked = true;
            else
                bHaveXueZhi.Checked = false;
            if (SysConfig.bHaveTimer)
                bHaveTimer.Checked = true;
            else
                bHaveTimer.Checked = false;
            if (SysConfig.bHaveZhiFangYi)
                bHaveZhiFangYi.Checked = true;
            else
                bHaveZhiFangYi.Checked = false;
        }

        private void cbShenGaoCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XPComNum = cbShenGaoCOM.Text;
            iniFileOP.Write("DeviceSet Set", "ShenGaoCOM", SysConfig.XPComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveShenGao_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveShenGao = bHaveShenGao.Checked;
            if (bHaveShenGao.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveShenGao);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveShenGao);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbTiWenCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.TiWenComNum = cbTiWenCOM.Text;
            iniFileOP.Write("DeviceSet Set", "TiWenCOM", SysConfig.TiWenComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveTiWen_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveTiWen = bHaveTiWen.Checked;
            if (bHaveTiWen.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveTiWen);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveTiWen);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbXueYaCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XueYaComNum = cbXueYaCOM.Text;
            iniFileOP.Write("DeviceSet Set", "XueYaCOM", SysConfig.XueYaComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveXueYa_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveXueYa = bHaveXueYa.Checked;
            if (bHaveXueYa.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueYa);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueYa);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbXinDianCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XinDianComNum = cbXinDianCOM.Text;
            iniFileOP.Write("DeviceSet Set", "XinDianCOM", SysConfig.XinDianComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveXinDian_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveXinDian = bHaveXinDian.Checked;
            if (bHaveXinDian.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXinDian);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXinDian);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbXueYangCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XueYangComNum = cbXueYangCOM.Text;
            iniFileOP.Write("DeviceSet Set", "XueYangCOM", SysConfig.XueYangComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveXueYang_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveXueYang = bHaveXueYang.Checked;
            if (bHaveXueYang.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueYang);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueYang);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbXueZhiCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XueZhiComNum = cbXueZhiCOM.Text;
            iniFileOP.Write("DeviceSet Set", "XueZhiCOM", SysConfig.XueZhiComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveXueZhi_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveXueZhi = bHaveXueZhi.Checked;
            if(bHaveXueZhi.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueZhi);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueZhi);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbXueTangCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.XueTangComNum = cbXueTangCOM.Text;
            iniFileOP.Write("DeviceSet Set", "XueTangCOM", SysConfig.XueTangComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveXueTang_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveXueTang = bHaveXueTang.Checked;
            if (bHaveXueTang.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueTang);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveXueTang);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void cbTimerCOM_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysConfig.TimerComNum = cbTimerCOM.Text;
            iniFileOP.Write("DeviceSet Set", "TimerCOM", SysConfig.TimerComNum, SysConfig.SysConfigFilePath);
        }

        private void bHaveTimer_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveTimer = bHaveTimer.Checked;
            if (bHaveTimer.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveTimer);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveTimer);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void bHaveZhiFangYi_CheckedChanged(object sender, EventArgs e)
        {
            SysConfig.bHaveZhiFangYi = bHaveZhiFangYi.Checked;
            if (bHaveZhiFangYi.Checked)
            {
                BytesOP.SetBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveZhiFangYi);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
            else
            {
                BytesOP.ClrBit(ref SysConfig.fjqd, (int)Common.FJQDIndex.bHaveZhiFangYi);
                SoftDataProtect.SetSoftData(SysConfig.fjqd.ToString(), 0);
            }
        }

        private void formDeviceSet_FormClosing(object sender, FormClosingEventArgs e)
        {
#if (Web)
            WebWork.Set_fujianqingdan();
#endif
            SysConfig.InitDeviceSet();
            SysConfig.OpenCOM();
        }
    }
}
